unit uMainForm;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
  ActnList, ComCtrls, DBGrids, ExtCtrls,dynlibs;

type
      POS_HANDLE = Pointer;

  { TfrmMainForm }

  TfrmMainForm = class(TForm)
    actClose: TAction;
    actJournal: TAction;
    actNewChequeue: TAction;
    ActionList: TActionList;
    Image1: TImage;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    procedure actCloseExecute(Sender: TObject);
    procedure actJournalExecute(Sender: TObject);
    procedure actNewChequeueExecute(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);


  private
    { private declarations }
  public
    { public declarations }
  end; 

var
  frmMainForm: TfrmMainForm;

implementation
uses ufrmSales, uDataModule;

{$R *.lfm}

{ TfrmMainForm }

procedure TfrmMainForm.actCloseExecute(Sender: TObject);
begin
  close;
end;

procedure TfrmMainForm.actJournalExecute(Sender: TObject);
begin
  DM.SQLConnector1.Connected:= true;
  //DM.SQLQuery1.ExecSQL;
  DM.SQLQuery1.Active:= true;
end;

procedure TfrmMainForm.actNewChequeueExecute(Sender: TObject);
begin
  frmSales := TfrmSales.Create(Application);
  frmSales.WindowState:= wsMaximized;
  frmSales.ShowModal;
end;

procedure TfrmMainForm.ToolButton3Click(Sender: TObject);
begin

end;

procedure TfrmMainForm.ToolButton4Click(Sender: TObject);
type
  TGetMessage = function(var handle_p:POS_HANDLE; const name:PChar; const log:PChar= NIl):ByteBool;cdecl;
  TPosClose = function (var handle_p: POS_HANDLE): ByteBool; cdecl;
  TPosSet = function (handle: POS_HANDLE; const param: PChar; const val: PChar): ByteBool; cdecl;
  TPosSend = function (handle: POS_HANDLE; action: Integer): ByteBool; cdecl;
  POS_HANDLE = Pointer;
var
  libFile : THandle;
  text1 : TGetMessage;// stdcall;
  PosClose : TPosClose;
  PosHandle : POS_HANDLE;
  PosSet : TPosSet;
  PosSend : TPosSend;
begin
  libFile := LoadLibrary(ExtractFilePath(Application.ExeName) + 'libpos.so');
  if libFile >= 32 then
  begin
  pointer(text1) := GetProcAddress(libFile,'pos_open');
   //@text1 := GetProcAddress(libFile,'pos_open');
   Text1 := TGetMessage(GetProcAddress(libFile,'pos_open'));
   PosClose := TPosClose(GetProcAddress(libFile,'pos_close'));
   PosSet := TPosSet(GetProcAddress(libFile,'pos_set'));
   PosSend := TPosSend(GetProcAddress(libFile,'pos_send'));


   if text1 <> nil then
   begin
     if not(text1(PosHandle,PChar('/dev/ttyUSB0'))) then ShowMessage('Error open com port.');
    posset(Poshandle, PChar('amount'), PChar('100'));
    posset(Poshandle, PChar('currency'), PChar('980')); { UAH  }
    PosSend(Poshandle,$0103);
    PosClose(PosHandle);

   end
   else exit;
   FreeLibrary(libFile);
  end
  else exit;
end;




end.

